<template>
  <div id="table_jsgl">
    <el-table :data="this.parent.data" border width="100%" :header-cell-style="{background:'#f9f9f9'}">
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="account" label="账号">
      </el-table-column>
      <el-table-column prop="name" label="姓名">
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称">
      </el-table-column>
      <el-table-column prop="accountStatus" label="账户状态" :formatter="AccountStatus">
      </el-table-column>
      <el-table-column prop="registrationTime" label="注册时间" :formatter="DateTime">
      </el-table-column>
      <el-table-column prop="loginTimes" label="登录次数">
      </el-table-column>
      <el-table-column prop="lastLoginTime" label="最后登录时间" :formatter="DateTime">
      </el-table-column>
      <el-table-column label="操作" width="250px">
        <template slot-scope="scope">
          <el-button size="small" plain @click="update(scope.row)">编辑</el-button>
          <el-button size="small" plain autofocus @click="dele(scope.row)">删除</el-button>
          <el-button size="small" plain autofocus @click="resetPws(scope.row)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑 -->
    <update1 ref="update1" :parent="this"></update1>

  </div>
</template>

<script>
  import update1 from "../update/update_glyyh.vue"
  export default {
    components: {
      update1,
    },
    props: ["parent"],
    methods: {
      //把row.roleManagementId里的字符串转换成整型
      roleIdInt(roleManagementId){
        if(roleManagementId != null){
          roleManagementId = roleManagementId.split(",");
          for(var i=0;i<roleManagementId.length;i++){
            roleManagementId[i] = parseInt(roleManagementId[i]);
          }
        }
        return roleManagementId;
      },
      // 刷新数据
      gettableData(){
        this.parent.getData();
      },
      // 编辑数据
      update(row) {
        row.roleManagementId = this.roleIdInt(row.roleManagementId);
        this.$refs.update1.idx = 0; //编辑
        this.$refs.update1.row = row; //传递数据
        // console.log(row);
        this.$refs.update1.dialogFormVisible = true; //弹出窗口
        this.$refs.update1.getTitle(); //获取title
      },

      // 删除数据
      dele(row) {
        // 弹窗
        this.$confirm("你确定删除这条信息吗？,删除后不可恢复", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => { //点击确定
          // 请求数据
          this.$axios.request({
            url: this.API + "/administrator/administratorUser/remove",
            params: {
              ids: row.id
            },
            method: "post",
            // 请求成功
          }).then((res) => {
            // console.log(res)
            if (res.data.code == 0) { // 删除成功
              this.parent.getData(); //重新请求数据
              this.$message.success("删除成功");
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }).catch(() => { //点击取消
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
      },
      // 重置密码
      resetPws(row) {
        // 弹窗
        this.$confirm("确认为该用户重置密码", "重置密码", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => { //点击确定
          // 请求数据
          this.$axios.request({
            url: this.API + "/administrator/administratorUser/resetPWD",
            params: {
              id: row.id
            },
            method: "post",
            // 请求成功
          }).then((res) => {
            // console.log(res)
            if (res.data.code == 0) { // 重置成功
              // this.parent.getData();   //重新请求数据
              // this.$message.success("重置密码成功");
              //显示重置的密码
              this.$alert('密码已重置为12345678,请提醒用户及时修改密码', '重置密码', {
                confirmButtonText: '确定',
                callback: action => {}
              });
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }).catch(() => { //点击取消
          this.$message({
            type: "info",
            message: "已取消重置密码"
          });
        });
      },
      //格式化账户状态
      AccountStatus(row,index){
        if(row.accountStatus == 1){
          return "正常";
        }else if(row.accountStatus == 0){
          return "冻结";
        }
      },
      //格式化日期时间
      DateTime(row,column){
        // console.log(column)
        var u = column.property;
        if(u == "registrationTime"){   //格式化注册时间
          if(row.registrationTime != null)
            return row.registrationTime.slice(0,10);
        }else if(u == "lastLoginTime"){  //格式化最后登录时间
          if(row.lastLoginTime != null){
            return row.lastLoginTime.slice(0,10);
          }
        }
        return "";
      },
    }
  }
</script>

<style>
  #admin_jsgl .el-table {
    margin-top: 10px;
    height: 100%;
  }

  #admin_jsgl .el-button.is-active {
    border: none;
  }
</style>
